﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Gollie.Graph;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class GraphControl : System.Web.UI.UserControl
	{
		Stats graph;

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		/*****************
		 * Transactions
		 *****************/
		public void FillGraph(Gollie.DataTables.VendorTransactions data)
		{
			if (!this.Check(data))
				return;

			this.FillStats(data);

			string url = this.graph.Url;

			this.imgGraph.ImageUrl = url;
		}

		private void FillStats(Gollie.DataTables.VendorTransactions data)
		{
			this.graph = new Stats("Transacties per dag", data.Count);

			this.graph.SetXLabels(data);

			StringCollection yLabels = new StringCollection();

			GraphDataSet usedDataSet = new GraphDataSet("Totaal", System.Drawing.Color.Blue);

			double highValue = 0;
			double lowValue = 0; // double.MaxValue;
			for (int i = 0; i < data.Count; i++)
			{
				double usedValue = Convert.ToDouble(data[i].Bedrag);

				if (-usedValue > highValue)
					highValue = -usedValue;

				usedDataSet.Values.Add(-usedValue);
			}

			int ticks = 5;
			double stepMultiple = 10;

			double q = (highValue - lowValue) / ((double)(ticks - 1));
			double stepSize = stepMultiple * Math.Ceiling(q / stepMultiple);

			double val = lowValue;
			for (int i = 0; i < ticks; i++)
			{
				yLabels.Add("€ " + ((int)(Math.Round(val, 0))).ToString("n0"));
				val += stepSize;
			}

			this.graph.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Left, labels = yLabels });

			this.graph.DataSets.Add(usedDataSet);

			this.graph.LowerBound = 0.0;
			this.graph.UpperBound = val - stepSize;
		}

		/*****************
		 * Dashboard
		 *****************/
		public void FillGraph(DepositsPerVendorPerPeriod data)
		{
			if (!this.Check(data))
				return;

			this.FillStats(data);
			
			string url = this.graph.Url;

			this.imgGraph.ImageUrl = url;
		}

		private void FillStats(DepositsPerVendorPerPeriod data)
		{
			this.graph = new Stats("Verstrekt / Verbruikt", data.Count);

			this.graph.SetXLabels(data);

			StringCollection yLabels = new StringCollection();

			GraphDataSet creditDataSet = new GraphDataSet("Verstrekt", System.Drawing.Color.Blue);
			GraphDataSet usedDataSet = new GraphDataSet("Verbruikt", System.Drawing.Color.Green);

			double highValue = 0;
			double lowValue = 0; // double.MaxValue;
			for (int i = 0; i<data.Count; i++)
			{
				double creditValue = Convert.ToDouble((decimal)data[i].Credit);
				double usedValue = Convert.ToDouble((decimal)data[i].Used);
				if (creditValue > highValue)
					highValue = creditValue;

				if (-usedValue > highValue)
					highValue = -usedValue;

				creditDataSet.Values.Add(creditValue);
				usedDataSet.Values.Add(-usedValue);
			}

			int ticks = 5;
			double stepMultiple = 10;

			double q = (highValue - lowValue) / ((double)(ticks - 1));
			double stepSize = stepMultiple * Math.Ceiling(q / stepMultiple);

			double val = lowValue;
			for (int i = 0; i < ticks; i++)
			{
				yLabels.Add("€ " + ((int)(Math.Round(val, 0))).ToString("n0"));
				val += stepSize;
			}

			this.graph.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Left, labels = yLabels });

			this.graph.DataSets.Add(creditDataSet);
			this.graph.DataSets.Add(usedDataSet);

			this.graph.LowerBound = 0.0;
			this.graph.UpperBound = val - stepSize;
		}

		private bool Check(IList data)
		{
			bool ok = data.Count <= 60;
			this.pnlError.Visible = !ok;
			this.pnlGraph.Visible = ok;

			return ok;
		}

		private class Stats : Graph.Graph
		{
			int count;

			public Stats(string title, int count)
				: base(title)
			{
				this.count = count;

				this.Width = 728;
				this.Height = 140;

				this.GridLines.BlankLength = 4;
				this.GridLines.LineLength = 4;
				this.GridLines.XStep = (double)100.0 / (double)(count);
				this.GridLines.YStep = 25;

				this.BackGroundColor = System.Drawing.Color.FromArgb(64, 255,255,255);
			}

			internal void SetXLabels(DepositsPerVendorPerPeriod data)
			{
				StringCollection xLabels1 = new StringCollection();
				StringCollection xLabels2 = new StringCollection();

				for (int i = 0; i < this.count; i++)
				{
					string l = data[i].Period;
					string l1 = l.Substring(2, 2);
					string l2 = l.Length > 5 ? l.Substring(5) : "";

					xLabels1.Add(l1);
					if (l2.Length > 0)
						xLabels2.Add(l2);
				}

				this.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Bottom, labels = xLabels1 });
				if (xLabels2.Count > 0)
					this.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Bottom, labels = xLabels2 });
			}

			internal void SetXLabels(Gollie.DataTables.VendorTransactions data)
			{
				StringCollection xLabels1 = new StringCollection();
				StringCollection xLabels2 = new StringCollection();
				StringCollection xLabels3 = new StringCollection();

				for (int i = 0; i < this.count; i++)
				{
					DateTime dt = data[i].Dag;
					string l1 = dt.ToString("yy");
					string l2 = dt.ToString("MM");
					string l3 = dt.ToString("dd");

					xLabels1.Add(l1);
					xLabels2.Add(l2);
					xLabels3.Add(l3);
				}

				this.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Bottom, labels = xLabels1 });
				this.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Bottom, labels = xLabels2 });
				this.AxisLabels.Add(new LabelValues { position = GraphAxisLabelsPosition.Bottom, labels = xLabels3 });
			}
		}

	}
}